package com.woniuxy.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.exception.certifiedException.CertifiedException;
import com.woniuxy.core.exception.certifiedException.CertifiedExceptionCode;
import com.woniuxy.core.exception.entranceException.EntranceException;
import com.woniuxy.core.exception.entranceException.EntranceExceptionCode;
import com.woniuxy.core.exception.orderException.OrderException;
import com.woniuxy.core.exception.orderException.OrderExceptionCode;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.order.dao.EntranceInfo;
import com.woniuxy.order.dto.CertifiedAccountInfoDto;
import com.woniuxy.order.feign.CertifiedClient;
import com.woniuxy.order.mapper.EntranceInfoMapper;
import com.woniuxy.order.model.dto.CertifiedInfo;
import com.woniuxy.order.model.dto.EntranceInfoDto;
import com.woniuxy.order.model.param.CarPullOutParam;
import com.woniuxy.order.model.param.LoadAllCarParam;
import com.woniuxy.order.service.EntranceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.order.service.OrderInfoService;
import com.woniuxy.order.service.impl.dto.OrderInfoDTO;
import com.woniuxy.order.service.param.AddCarParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.websocket.EncodeException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class EntranceInfoServiceImpl extends ServiceImpl<EntranceInfoMapper, EntranceInfo> implements EntranceInfoService {


    @Resource
    private CertifiedClient certifiedClient;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private EntranceInfoMapper entranceInfoMapper;

    @Override
    public void addCar(AddCarParam param, String id) throws ParseException {
        //管理员是否存在
        Result<CertifiedAccountInfoDto> accountInfo = certifiedClient.getAccountInfo(id);
        if(accountInfo==null){
            throw new CertifiedException(CertifiedExceptionCode.ACCOUNT_INEXISTENCE);
        }
        //该车牌号订单是否存在
        List<OrderInfoDTO> orderInfoDTOs = orderInfoService.selectOrderByCarPlate(param.getEntranceInfoPlate());
        EntranceInfo entranceInfo = new EntranceInfo();
        OrderInfoDTO orderInfoDTO = orderInfoDTOs.get(0);
        if(orderInfoDTO.getOrderInfoState()==6||orderInfoDTO.getOrderInfoState()==3){
            throw new OrderException(OrderExceptionCode.ORDER_INEXISTENCE);
        }
        //车辆订单是否已经付了定金
        if(orderInfoDTO.getOrderInfoState()!=2){
            throw new OrderException(OrderExceptionCode.CAR_CANNOT_INCOME);
        }
        //车辆驶入时间是否是订单开始时间和订单结束时间之内
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(param.getEntranceEnterTime());
        Instant instant = date.toInstant();
        //车辆实际驶入时间
        LocalDateTime enterTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //车辆订单开始时间
        LocalDateTime startTime = orderInfoDTO.getStartTime();
        //车辆订单结束时间
        LocalDateTime endTime = orderInfoDTO.getEndTime();
        if(enterTime.isBefore(startTime)||enterTime.isAfter(endTime)){
            throw new OrderException(OrderExceptionCode.CAR_CANNOT_INCOME1);
        }
        //用户信息id
        entranceInfo.setUserInfoId(orderInfoDTO.getUserInfoId());
        //物业信息id
        entranceInfo.setCertifiedInfoId(orderInfoDTO.getCentifiedInfoId());
        //物业信息
        Result<CertifiedInfo> result = certifiedClient.findCertifiedInfoById(orderInfoDTO.getCentifiedInfoId());
        String certifiedInfoName = result.getData().getCertifiedInfoName();
        //物业名称
        entranceInfo.setCertifiedInfoName(certifiedInfoName);
        //入场车牌号
        entranceInfo.setEntranceInfoPlate(param.getEntranceInfoPlate());
        //入场时间
        entranceInfo.setEntranceEnterTime(param.getEntranceEnterTime());
        //出场时间
        if(param.getEntranceOutTime()!=null||param.getEntranceOutTime()!=""){
            entranceInfo.setEntranceOutTime(param.getEntranceOutTime());
        }
        //车辆照片
        entranceInfo.setEntranceImg(param.getEntranceImg());

        entranceInfoMapper.insert(entranceInfo);
    }

    @Override
    public PageInfo<EntranceInfoDto> loadAllCar(LoadAllCarParam param, String id) {
        Page<EntranceInfo> page = new Page<>(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?5:param.getPageSize());
        //查询管理员所在的物业
//        Result<CertifiedInfo> result = certifiedClient.findCertifiedInfoById(Integer.valueOf(id));
//        CertifiedInfo certifiedInfo = result.getData();
        QueryWrapper<EntranceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_info_id",param.getCertifiedInfoId());
        //车牌号
        if(param.getEntranceInfoPlate()!=null||param.getEntranceInfoPlate()!=""){
            wrapper.like("entrance_info_plate",param.getEntranceInfoPlate());
        }
        //驶入时间,驶出时间
        if(!param.getEntranceEnterTime().equals("")&&!param.getEntranceOutTime().equals("")){
            wrapper.ge("entrance_enter_time",param.getEntranceEnterTime());
            wrapper.le("entrance_out_time",param.getEntranceOutTime());
        }
        wrapper.orderByDesc("entrance_info_id");
        entranceInfoMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public void deleteCar(Integer entranceInfoId) {
        //是否存在该车辆
        EntranceInfo entranceInfo = entranceInfoMapper.selectById(entranceInfoId);
        if(entranceInfoId==null){
            throw new EntranceException(EntranceExceptionCode.CAR_INEXISTENCE);
        }
        //删除
        entranceInfoMapper.deleteById(entranceInfoId);
    }

    @Override
    public void carPullOut(CarPullOutParam param) throws ParseException {
        //车辆是否存在
        EntranceInfo entranceInfo = entranceInfoMapper.selectById(param.getEntranceInfoId());
        if(entranceInfo==null){
            throw new EntranceException(EntranceExceptionCode.CAR_INEXISTENCE);
        }
        //车辆驶出时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(param.getEntranceOutTime());
        Instant instant = date.toInstant();
        LocalDateTime outTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //车辆驶入时间
        Date date1 = format.parse(entranceInfo.getEntranceEnterTime());
        Instant instant1 = date1.toInstant();
        LocalDateTime enterTime = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
        //车辆出场时间必须大于入场时间
        if(outTime.isBefore(enterTime)){
            throw new OrderException(OrderExceptionCode.CAR_CANNOT_OUT);
        }
        entranceInfo.setEntranceOutTime(param.getEntranceOutTime());
        entranceInfoMapper.updateById(entranceInfo);
    }
}
